package com.duojuhe.admin.spring.webconfig;


import com.duojuhe.common.utils.token.TokenUtils;
import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;



/**
 * Swagger文档配置
 *
 * @date 2018/2/9.
 */
@Profile("default")
@EnableSwagger2
@EnableKnife4j
@Configuration
public class SwaggerConfig {

    /** 是否开启swagger */
    @Value("${swagger.enabled}")
    private boolean enabled;

   /** 接口说明 */
    private final static String description = "<p>当前所有接口请求Body都是json格式</p>" +
            "<p>接口返回结果中errorCode（0=成功，其他一律弹窗提示message参数内容）</p>" +
            "<p>建议：当errorCode小于0 时请弹出提示后，重新定位到登录界面</p>" +
            "温馨提示：当接口文档中标识有参数的时候，如果所有参数都不填写的时候，请在body中传空json体{}，否则@RequestBody注解报异常";

    /** 需要token接口统一参数构建 */
    private static List<Parameter> parameters = new ArrayList<>();
    static  {
        parameters.add(new ParameterBuilder()
                .name(TokenUtils.TOKEN_HEADER)
                .description("认证token，登录成功后获得的返回值")
                .modelRef(new ModelRef("string"))
                .parameterType("header")
                .defaultValue("1")
                .required(true)
                .build());
    }

    @Bean(value = "basicApi")
    public Docket basicApi() {
        //分组名称
        String groupName = "01、无须登录公共接口";
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo(groupName, "1.0.0"))
                //分组名称
                .groupName(groupName)
                .enable(enabled)
                .select()
                //这里指定Controller扫描包路径
                .apis(RequestHandlerSelectors.basePackage("com.duojuhe.basic"))
                .paths(PathSelectors.any())
                .build();
    }

    @Bean(value = "systemApi")
    public Docket systemApi() {
        //分组名称
        String groupName = "02、系统模块接口";
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo(groupName, "1.0.1"))
                //分组名称
                .groupName(groupName)
                .globalOperationParameters(parameters)
                .enable(enabled)
                .select()
                //这里指定Controller扫描包路径
                .apis(RequestHandlerSelectors.basePackage("com.duojuhe.coremodule.system"))
                .paths(PathSelectors.any())
                .build();
    }


    @Bean(value = "formApi")
    public Docket formApi() {
        //分组名称
        String groupName = "03、问卷调查模块接口";
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo(groupName, "1.0.0"))
                //分组名称
                .groupName(groupName)
                .globalOperationParameters(parameters)
                .enable(enabled)
                .select()
                //这里指定Controller扫描包路径
                .apis(RequestHandlerSelectors.basePackage("com.duojuhe.coremodule.form"))
                .paths(PathSelectors.any())
                .build();
    }

    @Bean(value = "chatApi")
    public Docket chatApi() {
        //分组名称
        String groupName = "04、多聊模块接口";
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo(groupName, "1.0.0"))
                //分组名称
                .groupName(groupName)
                .globalOperationParameters(parameters)
                .enable(enabled)
                .select()
                //这里指定Controller扫描包路径
                .apis(RequestHandlerSelectors.basePackage("com.duojuhe.coremodule.chat"))
                .paths(PathSelectors.any())
                .build();
    }


    @Bean(value = "workflowApi")
    public Docket workflowApi() {
        //分组名称
        String groupName = "05、工作流模块接口";
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo(groupName, "1.0.0"))
                //分组名称
                .groupName(groupName)
                .globalOperationParameters(parameters)
                .enable(enabled)
                .select()
                //这里指定Controller扫描包路径
                .apis(RequestHandlerSelectors.basePackage("com.duojuhe.coremodule.workflow"))
                .paths(PathSelectors.any())
                .build();
    }

    @Bean(value = "helpApi")
    public Docket helpApi() {
        //分组名称
        String groupName = "06、帮助中心模块接口";
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo(groupName, "1.0.0"))
                //分组名称
                .groupName(groupName)
                .globalOperationParameters(parameters)
                .enable(enabled)
                .select()
                //这里指定Controller扫描包路径
                .apis(RequestHandlerSelectors.basePackage("com.duojuhe.coremodule.help"))
                .paths(PathSelectors.any())
                .build();
    }

    @Bean(value = "infoApi")
    public Docket infoApi() {
        //分组名称
        String groupName = "07、信息中心模块接口";
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo(groupName, "1.0.0"))
                //分组名称
                .groupName(groupName)
                .globalOperationParameters(parameters)
                .enable(enabled)
                .select()
                //这里指定Controller扫描包路径
                .apis(RequestHandlerSelectors.basePackage("com.duojuhe.coremodule.info"))
                .paths(PathSelectors.any())
                .build();
    }

    @Bean(value = "noticeApi")
    public Docket noticeApi() {
        //分组名称
        String groupName = "08、通知公告模块接口";
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo(groupName, "1.0.0"))
                //分组名称
                .groupName(groupName)
                .globalOperationParameters(parameters)
                .enable(enabled)
                .select()
                //这里指定Controller扫描包路径
                .apis(RequestHandlerSelectors.basePackage("com.duojuhe.coremodule.notice"))
                .paths(PathSelectors.any())
                .build();
    }

    @Bean(value = "quartzApi")
    public Docket quartzApi() {
        //分组名称
        String groupName = "09、定时调度任务模块接口";
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo(groupName, "1.0.0"))
                //分组名称
                .groupName(groupName)
                .globalOperationParameters(parameters)
                .enable(enabled)
                .select()
                //这里指定Controller扫描包路径
                .apis(RequestHandlerSelectors.basePackage("com.duojuhe.coremodule.quartz"))
                .paths(PathSelectors.any())
                .build();
    }

    @Bean(value = "smsApi")
    public Docket smsApi() {
        //分组名称
        String groupName = "10、短信平台模块接口";
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo(groupName, "1.0.0"))
                //分组名称
                .groupName(groupName)
                .globalOperationParameters(parameters)
                .enable(enabled)
                .select()
                //这里指定Controller扫描包路径
                .apis(RequestHandlerSelectors.basePackage("com.duojuhe.coremodule.sms"))
                .paths(PathSelectors.any())
                .build();
    }


    @Bean(value = "payApi")
    public Docket payApi() {
        //分组名称
        String groupName = "11、支付中心模块接口";
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo(groupName, "1.0.0"))
                //分组名称
                .groupName(groupName)
                .globalOperationParameters(parameters)
                .enable(enabled)
                .select()
                //这里指定Controller扫描包路径
                .apis(RequestHandlerSelectors.basePackage("com.duojuhe.coremodule.pay"))
                .paths(PathSelectors.any())
                .build();
    }

    @Bean(value = "topoApi")
    public Docket topoApi() {
        //分组名称
        String groupName = "12、拓扑图模块接口";
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo(groupName, "1.0.0"))
                //分组名称
                .groupName(groupName)
                .globalOperationParameters(parameters)
                .enable(enabled)
                .select()
                //这里指定Controller扫描包路径
                .apis(RequestHandlerSelectors.basePackage("com.duojuhe.coremodule.topo"))
                .paths(PathSelectors.any())
                .build();
    }

    @Bean(value = "trackingApi")
    public Docket trackingApi() {
        //分组名称
        String groupName = "13、产品溯源模块接口";
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo(groupName, "1.0.0"))
                //分组名称
                .groupName(groupName)
                .globalOperationParameters(parameters)
                .enable(enabled)
                .select()
                //这里指定Controller扫描包路径
                .apis(RequestHandlerSelectors.basePackage("com.duojuhe.coremodule.tracking"))
                .paths(PathSelectors.any())
                .build();
    }

    @Bean(value = "monitorApi")
    public Docket monitorApi() {
        //分组名称
        String groupName = "14、系统监控模块接口";
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo(groupName, "1.0.0"))
                //分组名称
                .groupName(groupName)
                .globalOperationParameters(parameters)
                .enable(enabled)
                .select()
                //这里指定Controller扫描包路径
                .apis(RequestHandlerSelectors.basePackage("com.duojuhe.coremodule.monitor"))
                .paths(PathSelectors.any())
                .build();
    }



    private ApiInfo apiInfo(String name,String version) {
        return new ApiInfoBuilder()
                .title(name)
                .version(version)
                .termsOfServiceUrl("http://api.duojuhe.com")
                .contact(new Contact("多聚合", "http://www.duojuhe.com", "13637050677@qq.com"))
                .description(description)
                .license("README.md")
                .licenseUrl("https://gitee.com/duojuhe/duojuheAdmin")
                .build();
    }

}
